package com.scanner.hardware;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.scanner.bean.Constans;
import com.scanner.bean.DeviceInfo;
import com.scanner.bean.ScanSetting;
import com.scanner.util.BaseUtils;
import com.scanner.util.LogUtil;
import com.scanner.util.SharePreConfig;

import org.angmarch.views.NiceSpinner;

import java.util.ArrayList;
import java.util.List;

public class ChooseScannerActivity extends Activity {

    private List<DeviceInfo> dlist;
    private int jump_type;
    private LinearLayout ll_back;
    private NiceSpinner nsDevice;
    private int dP = 0;//默认设备选择
    private int deviceType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_scanner);
        Intent intent = getIntent();
        if(intent!=null){
            jump_type = intent.getIntExtra("jump_type", 0);
        }
        initData();
        initView();

    }



    private void initData() {
        dlist = new ArrayList<>();
        DeviceInfo deviceInfo = new DeviceInfo(Constans.DTMoToSE655, Constans.MotoOne, 9600);
        DeviceInfo deviceInfo1 = new DeviceInfo(Constans.DTMoTOSE2707, Constans.MotoTWo, 9600);
        DeviceInfo deviceInfo2 = new DeviceInfo(Constans.DTHONEYWELLONE, Constans.HONEYWELLONE, 9600);//霍尼韦尔 1D :115200
        DeviceInfo deviceInfo3 = new DeviceInfo(Constans.DTHONEYWELL, Constans.HONEYWELLTwo, 115200);
        dlist.add(deviceInfo);
        dlist.add(deviceInfo1);
        dlist.add(deviceInfo2);
        dlist.add(deviceInfo3);
        deviceType = Constans.DTMoToSE655;
    }

    private void initView() {
        nsDevice = findViewById(R.id.ns_deviceMode);

        findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showDialog();
            }
        });
        ll_back = findViewById(R.id.ll_back);
        nsDevice.attachDataSource(dlist);
        deviceType = BaseUtils.getSettingFormShare().getScanModule();//获取默认的设备形式。

        switch (deviceType) {
            case Constans.DTHONEYWELL:
                dP = 3;
                break;
            case Constans.DTHONEYWELLONE:
                dP = 2;
                break;
            case Constans.DTMoTOSE2707:
                dP = 1;
                break;
            case Constans.DTMoToSE655:
                dP = 0;
                break;
        }
        nsDevice.setSelectedIndex(dP);
        LogUtil.e("选择了fff"+dP+";type="+deviceType);
        nsDevice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                dP = position;
                dP = nsDevice.getSelectedIndex();

                deviceType = dlist.get(dP).getDeviceType();
                LogUtil.e("选择了fff"+dP+";type="+deviceType);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        if(jump_type == 1){
            ll_back.setVisibility(View.VISIBLE);
        }

        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void showDialog(){
        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setIcon(R.mipmap.icon_tm);//设置对话框
        dialog.setTitle(getString(R.string.sure_modlue));//设置对话框标题
        dialog.setMessage(getString(R.string.sure_module_contents));//设置文字显示内容
        //分别设置三个
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.sure), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//关闭对话框
                //数据保存本地
                SharePreConfig.setIsFirstOpen(false);
                LogUtil.e("位置"+dP);
//                SharePreConfig.setDeviceType(dlist.get(dP).getDeviceType());

                ScanSetting scanSetting = BaseUtils.getSettingFormShare();
                scanSetting.setScanModule(dlist.get(dP).getDeviceType());
                BaseUtils.beanToString(scanSetting);

                LogUtil.e(";测试保存的数据=="+dlist.get(dP).getDeviceType());
                SharePreConfig.setBaudRate(dlist.get(dP).getBaudRate());
                Intent intent = new Intent(ChooseScannerActivity.this,StartActivity.class);
                intent.putExtra("deviceName",dlist.get(dP).getDeviceType());
                ChooseScannerActivity.this.startActivity(intent);
                ChooseScannerActivity.this.finish();
            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.str_cancle), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//关闭对话框
            }
        });
        dialog.show();//显示对话框
    }
}
